//
//  NetworkingTolls.swift
//  XLWB
//
//  Created by 郭亮 on 16/3/15.
//  Copyright © 2016年 gl. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestStyle: String {
    case RequestStyleGet = "GET"
    case RequestStylePost = "POST"
}

class NetworkingTolls: AFHTTPSessionManager {
    // MARK:-单例
    static let shareInstance: NetworkingTolls = {
        let tools = NetworkingTolls()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
}

extension NetworkingTolls {
    func request(requestStyle: RequestStyle, urlString: String, parameters: [String: AnyObject], finishedBlock: (result: AnyObject?, error: NSError?) -> ()) {
        
        //成功回调
        let successBlock = { (task: NSURLSessionDataTask, result: AnyObject?) -> Void in
            finishedBlock(result: result, error: nil)
        }
        
        //失败回调
        let failureBlock = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            finishedBlock(result: nil, error: error)
        }
        
        if requestStyle == .RequestStyleGet {
            GET(urlString, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
        
        if requestStyle == .RequestStylePost {
        POST(urlString, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
    }
}
